﻿using NDK.Motion.Card;
using NDK.Motion.MachineManager;
using NDK.Motion.MachineResources;
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace NDK.Motion.Frm
{
    public partial class Dlg_CardDebug : Form
    {
        private Dlg_CardDebug()
        {
            InitializeComponent();
        }
        private static Dlg_CardDebug _instance;
        private static readonly object singletonLock = new object();
        public static Dlg_CardDebug Instance
        {
            get
            {
                if (_instance == null || _instance.IsDisposed)
                {
                    lock (singletonLock)
                    {
                        if (_instance == null || _instance.IsDisposed)
                        {
                            _instance = new Dlg_CardDebug();
                        }
                    }
                }
                return _instance;
            }
        }
        private void Dlg_CardDebug_Load(object sender, EventArgs e)
        {
            dgv_CardPara.AutoGenerateColumns = false;
            dgv_CardPara.DataSource = new BindingList<ICard>(MachineDataManager.Instance.CardList);
        }

        private void cms_Card_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            var rows = dgv_CardPara.SelectedRows;
            var cardId = int.Parse(rows[0].Cells[0].Value.ToString());
            var cardName = rows[0].Cells[1].Value.ToString();
            var cardType = rows[0].Cells[2].Value.ToString();
            ICard card = MachineDataManager.Instance.CardList.Find(x => x.CardID == (int)cardId && x.CardName == cardName && x.CardType == cardType);
            if (card != null)
            {
                //ICard card 
                if (e.ClickedItem == tsmi_AxisUpdate)//Axis配置
                {

                }
                if (e.ClickedItem == tsmi_IOUpdate)//Axis配置
                {
                    int inputCount = card.InputCount;
                    int outputCount = card.OutputCount;
                    card.UpdateSlaveResource(ref card);
                    if (inputCount < card.InputCount)
                    {
                        for (int port = 0; port < card.InputCount; port++)
                        {
                            if (port < inputCount)
                                continue;
                            string defaultName = "DI" + (port + 1) + "(" + card.CardType + "_" + card.CardID + ")";//默认名称
                            InputSetting inputModel = new InputSetting(defaultName, card, port);
                            if (MachineDataManager.Instance.AddDI(inputModel))
                            {

                            }
                        }
                    }
                    if (outputCount < card.OutputCount)
                    {
                        for (int port = 0; port < card.OutputCount; port++)
                        {
                            if (port < outputCount)
                                continue;
                            string defaultDOName = "DO" + (port + 1) + "(" + card.CardID + ")";//默认名称
                            OutputSetting outputModel = new OutputSetting(defaultDOName, card, port);
                            if (MachineDataManager.Instance.AddDO(outputModel))
                            {
                            }
                        }
                    }
                }
            }

        }
    }
}
